using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Configuration;


namespace DAL
{
    public class tbTinhTrangThanhToanDAL
    {
        private SqlConnection cn = null;
        public tbTinhTrangThanhToanDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbTinhTrangThanhToanDAL(tbTinhTrangThanhToanDAL tan)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbTinhTrangThanhToan th)
        {
            SqlCommand cm = new SqlCommand("tbTinhTrangThanhToan_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tentttt", th.tentt);   
            cm.ExecuteNonQuery();
        }
        public void Update(tbTinhTrangThanhToan th)
        {
            SqlCommand cm = new SqlCommand("tbTinhTrangThanhToan_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matttt", th.matttt);
            cm.Parameters.AddWithValue("@tentt", th.tentt);  
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbTinhTrangThanhToan_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matttt", iD);
            cm.ExecuteNonQuery();
        }
        public tbTinhTrangThanhToanCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbTinhTrangThanhToan_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbTinhTrangThanhToanCollection thanhtoanCollection = new tbTinhTrangThanhToanCollection();

            while (dr.Read())
            {
                tbTinhTrangThanhToan th = new tbTinhTrangThanhToan();
                th.matttt = int.Parse(dr.GetValue(0).ToString());
                th.tentt = dr.GetValue(1).ToString();
                thanhtoanCollection.Add(th);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return thanhtoanCollection;
        }
    }
}

